#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "widgets_init.h"
#include "custom.h"
#include "app_lvglDeal.h"

const char *getDeviceStatusString(uint8_t devStatus, lv_obj_t *obj)
{
    switch (devStatus)
    {
    case 0:
        lv_obj_set_style_bg_color(obj, lv_color_hex(0x70ffc2), LV_PART_MAIN | LV_STATE_DEFAULT);
        return "设备正常";
    case 1:
        lv_obj_set_style_bg_color(obj, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
        return "报警";
    case 2:
        lv_obj_set_style_bg_color(obj, lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT);
        return "离线";
    }
}

lv_obj_t *createButton(lv_obj_t *parent, const char *text, int posY)
{
    lv_obj_t *btn = lv_btn_create(parent);
    lv_obj_t *label = lv_label_create(btn);
    lv_label_set_text(label, text);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_width(label, LV_PCT(100));
    lv_obj_set_pos(btn, 372, posY);
    lv_obj_set_size(btn, 100, 50);

    lv_obj_set_style_bg_opa(btn, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0x2195f6), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0x1a7bbf), LV_PART_MAIN | LV_EVENT_CLICKED); // 按下状态颜色
    lv_obj_set_style_bg_grad_dir(btn, LV_GRAD_DIR_NONE, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(btn, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(btn, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(btn, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(btn, lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(btn, &lv_font_SourceHanSerifSC_Regular_16, LV_PART_MAIN | LV_STATE_DEFAULT);
    return btn;
}

void setup_scr_screen_main(lv_ui *ui)
{
    ui->screen_main = lv_obj_create(NULL);
    lv_obj_set_size(ui->screen_main, 480, 320);
    lv_obj_set_scrollbar_mode(ui->screen_main, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_opa(ui->screen_main, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->screen_main, lv_color_hex(0xb7fff8), LV_PART_MAIN | LV_STATE_DEFAULT);

    ui->screen_main_btn_1 = createButton(ui->screen_main, "wifi配置", 36);
    ui->screen_main_btn_2 = createButton(ui->screen_main, "关于本机", 126);
    ui->screen_main_btn_3 = createButton(ui->screen_main, "节点数据", 216);

    ui->screen_main_img_1 = lv_img_create(ui->screen_main);
    lv_img_set_src(ui->screen_main_img_1, &_4G_alpha_34x29);
    lv_obj_set_pos(ui->screen_main_img_1, 3, 2);
    lv_obj_set_size(ui->screen_main_img_1, 34, 29);

    ui->screen_main_cont_1 = lv_obj_create(ui->screen_main);
    lv_obj_set_pos(ui->screen_main_cont_1, -1, 31);
    lv_obj_set_size(ui->screen_main_cont_1, 365, 239);
    lv_obj_set_style_border_color(ui->screen_main_cont_1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui->screen_main_cont_1, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    const char *statusText = getDeviceStatusString(userLvglPar.devStatus, ui->screen_main_cont_1);
    ui->screen_main_label_1 = lv_label_create(ui->screen_main);
    lv_label_set_text(ui->screen_main_label_1, userLvglPar.mainDevNum);
    lv_obj_set_pos(ui->screen_main_label_1, 227, 36);
    lv_obj_set_style_text_color(ui->screen_main_label_1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->screen_main_label_1, &lv_font_SourceHanSerifSC_Regular_26, LV_PART_MAIN | LV_STATE_DEFAULT);

    char temp_str[6];
    snprintf(temp_str, sizeof(temp_str), "%5.1f", userLvglPar.mainTemp);
    ui->screen_main_label_3 = lv_label_create(ui->screen_main);
    lv_label_set_text(ui->screen_main_label_3, temp_str);
    lv_obj_set_pos(ui->screen_main_label_3, 107, 86);
    lv_obj_set_style_text_font(ui->screen_main_label_3, &lv_font_SourceHanSerifSC_Regular_50, LV_PART_MAIN | LV_STATE_DEFAULT);

    snprintf(temp_str, sizeof(temp_str), "%5.1f", userLvglPar.mainHumi);
    ui->screen_main_label_2 = lv_label_create(ui->screen_main);
    lv_label_set_text(ui->screen_main_label_2, temp_str);
    lv_obj_set_pos(ui->screen_main_label_2, 107, 135);
    lv_obj_set_style_text_font(ui->screen_main_label_2, &lv_font_SourceHanSerifSC_Regular_50, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui->screen_main_label_4 = lv_label_create(ui->screen_main);
    lv_label_set_text(ui->screen_main_label_4, statusText);
    lv_obj_set_pos(ui->screen_main_label_4, 4, 246);
    lv_obj_set_style_text_font(ui->screen_main_label_4, &lv_font_SourceHanSerifSC_Regular_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_update_layout(ui->screen_main);
    events_init_screen_main(ui);
}
